package com.young.design.pattern.singleton;

/**
 * @ClassName : SingleTon7
 * @Author : hyy
 * @Date: 2020-03-14
 * 饿汉式---枚举 枚举类型不允许被继承，同样是线程安全的，并且只能被实例化一次，但是枚举类型不能够实现懒加载
 */
public class SingleTon2 {
    private SingleTon2(){

    }

    public static SingleTon2 getInstance(){
       return EnumSingleTon.INSTANCE.getInstance();
    }

    public  enum EnumSingleTon{
        INSTANCE;
        private SingleTon2 singleTon7;

        private EnumSingleTon(){
            singleTon7 = new SingleTon2();
        }

        private SingleTon2 getInstance(){
            return singleTon7;
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                System.out.println(SingleTon2.getInstance());
            },String.valueOf(i)).start();
        }
    }
}
